import javafx.util.Pair;

import java.util.TreeMap;

/**
* @description: 981. 基于时间的键值存储
* @author hewei
* @date 2022/9/29 16:02
* @version 1.0
*/

public class TimeMap {

    TreeMap<Pair<String, Integer>, String> map;
    public TimeMap() {
        map = new TreeMap<>((a, b) -> {
            if (a.getKey().equals(b.getKey())) {
                return a.getValue() - b.getValue();
            } else return a.getKey().compareTo(b.getKey());
        });
    }

    public void set(String key, String value, int timestamp) {
        map.put(new Pair<>(key, timestamp), value);
    }

    public String get(String key, int timestamp) {
        Pair<String, Integer> pair = map.floorKey(new Pair<>(key, timestamp));
        if (pair == null || !pair.getKey().equals(key)) return "";
        return map.get(pair);
    }
}
